
/*
 * isNoLoading    //没有加载提示
 * isNoErrTips    //
 * isNoSuccessTips
 * */
const http = {
  baseUrl: process.env.VUE_APP_BASEURL,
  request(config) {
    if (!config.isNoLoading) {
      uni.showLoading({ title: "" });
    }
    config = beforeRequest(config);
    config.url = this.baseUrl + config.url;
    return new Promise((resolve, reject) => {
      uni.request(config).then((res) => {
          if (!config.isNoLoading) {
            uni.hideLoading();
          }
          const response = beforeResponse(res.data);
          if (response.code === 401) {
            reject(response);
            return;
          }
          if (response.code !== 200) {
            if (!config.isNoErrTips) {
              uni.showToast({ title: response.msg, duration: 2000, icon: "none", });
            }
            reject(response);
            return;
          }
          if (!config.isNoSuccessTips) {
            uni.showToast({ title: response.msg, duration: 2000 });
          }
          resolve(response);
        }).catch((err) => {
          if (!config.isNoLoading) {
            uni.hideLoading();
          }
          if (!config.isNoErrTips) {
            uni.showToast({ title: "接口错误", duration: 2000, icon: "none" });
          }
          errorHandle(err);
          reject(err);
        });
    });
  },
  get(data) {
    return this.request({ method: "GET", ...data });
  },
  post(data) {
    return this.request({ method: "POST", ...data });
  },
};
const beforeRequest = (config) => {

  if (!config.header) {
    // config.header = {'Content-Type': 'application/json'};
    config.header = { };
  }

  //是否带上token
  if (config.noToken) {
    delete config.noToken;
    return config;
  }
  const token = uni.getStorageSync('token')
  config.header.Token = token
  return config;
};
const beforeResponse = (response) => {
  return response;
};
const errorHandle = (err) => {
  return err;
};
export default http;
